package com.jierui.meter.frontend.service.impl;

import com.jierui.meter.frontend.domain.BeanUtil;
import com.jierui.meter.frontend.domain.DO.MeterStatusDO;
import com.jierui.meter.frontend.domain.DO.ShowIndicesDO;
import com.jierui.meter.frontend.domain.DO.TErrorComputingResultsDO;
import com.jierui.meter.frontend.domain.Param.ErrorComputingParam;
import com.jierui.meter.frontend.domain.VO.CalInfoVO;
import com.jierui.meter.frontend.domain.VO.InvalidMeterTgVO;
import com.jierui.meter.frontend.domain.VO.ResultTableVO;
import com.jierui.meter.frontend.mapper.CalInfoMapper;
import com.jierui.meter.frontend.mapper.MeterStatusMapper;
import com.jierui.meter.frontend.mapper.ShowIndicesMapper;
import com.jierui.meter.frontend.mapper.TErrorComputingMapper;
import com.jierui.meter.frontend.service.CalInfoService;
import com.jierui.meter.frontend.service.ShowIndicesService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 计算结果
 * @author: wangjingjing
 * @creat: 2021-06-17 10:59
 */
@Service
public class CalInfoServiceImpl implements CalInfoService {

    @Autowired
    CalInfoMapper calInfoMapper;
    private static final Log log = LogFactory.getLog(CalInfoServiceImpl.class);

    @Autowired
    ShowIndicesMapper indicesMapper;

    @Autowired
    MeterStatusMapper meterStatusMapper;

    @Autowired
    TErrorComputingMapper errorComputingMapper;

    @Autowired
    ShowIndicesService showIndicesService;

    @Override
    public Map<String, Object> getResult(String year, String scope, String orgNo7, Integer page, Integer size) {
        Map<String, Object> result = new HashMap<>();
        List<ShowIndicesDO> dos = indicesMapper.selectByDateAndON57(year, scope, orgNo7, (page - 1) * size, size);
        //查询总条数
        int count = indicesMapper.selectByDateAndON57Count(year, scope, orgNo7);


        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int)Math.ceil(count/size));

        List<CalInfoVO> data = new ArrayList<>();
        if (dos.isEmpty()) {
            result.put("list", data);
            return result;
        }

        dos.stream().forEach(showIndicesDO -> {
            data.add(BeanUtil.showIndicesDO2CalInfoVO(showIndicesDO));
        });
        result.put("list", data);
        return result;
    }

    @Override
    public List<ResultTableVO> getResultNoPage(String year, String scope, String orgNo7) {
        List<ShowIndicesDO> dos = indicesMapper.selectByDateAndON57GroupByOrg7Name(year, scope, orgNo7);

        List<ResultTableVO> data = new ArrayList<>();
        if (dos.isEmpty()) {
            return data;
        }

        dos.stream().forEach(showIndicesDO -> {
            data.add(BeanUtil.showIndicesDO2ResultTableVO(showIndicesDO));
        });
        return data;
    }

    @Override
    public Map<String, Object> getDashboard(String year, String scope, String orgNo7) {
        Map<String,Object> result = new HashMap<>();
        //如果orgNo 不为空 正常数据只有一条
        ShowIndicesDO indicesDO = showIndicesService.selectByDateAndON7SUM(year, scope, orgNo7);

        result.put("tg_num", indicesDO.getNTg());
        result.put("meter_num", indicesDO.getNMeter());
        if (indicesDO.getNTg() > 0) {
            float tg_rate = Math.round((indicesDO.getNNormTg() + indicesDO.getNErrTg()) / (float) indicesDO.getNTg() * 10000) / 10000F;
            result.put("valid_tg_rate", tg_rate);
        } else {
            result.put("valid_tg_rate", 0f);
        }
        //超差电表数 正常台区超差表数量 + 新建台区超差表数量 + 异常台区超差表数量
        int invalidMeterNum = indicesDO.getNNormTgErrMeter() + indicesDO.getNNewTgErrMeter() + indicesDO.getNErrTgErrMeter();
        result.put("invalid_meter_num", invalidMeterNum);
        //电表超差率
        if (indicesDO.getNMeter() > 0) {
            result.put("invalid_meter_rate", Math.round(invalidMeterNum / (float) indicesDO.getNMeter() * 10000) / 10000F);
        } else {
            result.put("invalid_meter_rate",0f);
        }
        result.put("alarm_event_num", 0); //待处理异常数

        //查询台区下所有地区 的超差电表数量和台区可算率
        List<ShowIndicesDO> showIndicesDOS = indicesMapper.selectByDateAndON57GroupByOrg7Name(year, scope, orgNo7);

        List<InvalidMeterTgVO> invalidMeterList = new ArrayList<>();

        if (!showIndicesDOS.isEmpty()) {
            showIndicesDOS.stream().forEach(showIndicesDO -> {
                InvalidMeterTgVO invalidMeterTgVO = new InvalidMeterTgVO();
                invalidMeterTgVO.setName(showIndicesDO.getOrg7Name().trim());
                invalidMeterTgVO.setNum(showIndicesDO.getNErrMeter());
                invalidMeterTgVO.setRate(Math.round(showIndicesDO.getNErrMeter() / (float) indicesDO.getNMeter() * 10000F) / 10000F);
                invalidMeterList.add(invalidMeterTgVO);
            });

        }
        result.put("invalid_meter_tg", invalidMeterList);

        //超差电表厂家分布 meter_status 按厂商分组
        List<MeterStatusDO> meterStatusDOS = meterStatusMapper.selectByON7GroupByFirm(orgNo7, year);
        List<InvalidMeterTgVO> meterNumByFirmList = new ArrayList<>();
        if (!meterStatusDOS.isEmpty()) {
            meterStatusDOS.stream().forEach(meterStatusDO -> {
                InvalidMeterTgVO invalidMeterTgVO = new InvalidMeterTgVO();
                if (StringUtils.isNotBlank(meterStatusDO.getMANUFACTURER())) {
                    invalidMeterTgVO.setName(meterStatusDO.getMANUFACTURER().trim());
                }
                invalidMeterTgVO.setNum(meterStatusDO.getMeterNum());
                meterNumByFirmList.add(invalidMeterTgVO);
            });
        }
        result.put("invalid_meter_manufacturers", meterNumByFirmList);

        //超差电表运行年限分布
        List<MeterStatusDO> runTimeDOS = meterStatusMapper.selectByON7GroupByRunTime(orgNo7, year);
        List<InvalidMeterTgVO> meterRunTimeList = new ArrayList<>();
        if (!runTimeDOS.isEmpty()) {
            runTimeDOS.stream().forEach(meterStatusDO -> {
                InvalidMeterTgVO invalidMeterTgVO = new InvalidMeterTgVO();
                invalidMeterTgVO.setName(String.valueOf(meterStatusDO.getNDAYS()));
                invalidMeterTgVO.setNum(meterStatusDO.getMeterNum());
                meterRunTimeList.add(invalidMeterTgVO);
            });
        }
        result.put("invalid_meter_running_time", meterRunTimeList);


        ErrorComputingParam param = new ErrorComputingParam();
        param.setYear(year);
        param.setOrgNo7(orgNo7);
        //超差电表按异常信息分组
        List<InvalidMeterTgVO> alarmCodeResultList = new ArrayList<>();
        List<TErrorComputingResultsDO> alarmCodeList = errorComputingMapper.selectByAlarmCodeDistinctMeterId(param);
        if (!alarmCodeList.isEmpty()) {
            int totalAlramNum = alarmCodeList.stream().map(dto -> dto.getAlarmNum()).reduce(Integer::sum).get();
            alarmCodeList.stream().forEach(meterStatusDO -> {
                InvalidMeterTgVO invalidMeterTgVO = new InvalidMeterTgVO();
                invalidMeterTgVO.setName(String.valueOf(meterStatusDO.getAlarmCode()));
                invalidMeterTgVO.setNum(meterStatusDO.getAlarmNum());
                invalidMeterTgVO.setRate(Math.round(meterStatusDO.getAlarmNum() * 1F / totalAlramNum * 10000) / 10000f);
                alarmCodeResultList.add(invalidMeterTgVO);
            });
        }
        result.put("invalid_meter_type", alarmCodeResultList);


        return result;
    }
}
